<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;


class Post extends Controller
{
    
    public function index()
    {
        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();

        $session = Session::get('user_info');   
        $list = Db::name('post')->select();
        // var_dump($list);
        $roles = array();
        foreach ($list as $val)
        {
            $num = Db::name('postcomment')->where('post_id',$val['post_id'])->count();
            // var_dump($num);
            // var_dump($user['name']);
            $val['num'] = $num;
            $roles[] = $val;

        }

        $posthot = Db::name('post')->where('posthot',0)->select();
        $links = Db::name('links')->select();
        $count = Db::name('postcomment')->count();//帖子评论个数
        // var_dump($roles);
        // die;
        return view('post/index',[
            'lists' => $roles,
            'posthot' => $posthot,
            'session' => $session,
            'links' => $links,
            'user_id' => $info['user_id'],
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts
        ]);

    }

    public function addpost()
    {


        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();

        return view('post/addpost',[
            'user_id' => $info['user_id'],//登录处理
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts
        ]);
        // return view('post/addpost');

    }

    public function add(Request $request)
    {   

        // $session = Session::get('user_info');
        $p = $request->post();
        $session = Session::get('user_info');
        $user_id = $session['user_id'];

        //发评论增加积分
        $user = Db::name('index_user')->find($user_id);
        $bp = $user['bp'] + 5;
        $userdata = [
            'bp' => $bp
        ];
        Db::name('index_user')->where('user_id',$user_id)->update($userdata);
        //发评论增加积分
        $users = Db::name('index_user')->find($user_id);
        Session::delete('user_info');
        Session::set('user_info',$users);
        $data = [
            'user_id' =>  $user_id,
            'postname' => $p['postname'],
            'posttime' => date('Y-m-d h:m:s'),
            'postcontent' => $p['postcontent'],
            'posttext' => $p['posttext'],
            'artpic' => $p['picname']

        ];
        $result = Db::name('post')->data($data)->insert();
        return $this->redirect('index/post/index');
    }

    //图片上传的ajax操作
    public function server()
    {
        $name = isset($_POST['name'])? $_POST['name'] : '';
        $gender = isset($_POST['gender'])? $_POST['gender'] : '';

        $filename = md5(rand()).time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));

        $response = array();

        if(move_uploaded_file($_FILES['photo']['tmp_name'], './static/postpic/'.$filename)){
            $response['isSuccess'] = true;
            $response['name'] = $name;
            $response['gender'] = $gender;
            $response['photo'] = $filename;

        }else{
            $response['isSuccess'] = false;
        }

        echo json_encode($response);
    }

    public function detail($id)
    {
        //登录处理
        $info = Session::get('user_info');

        $user_id = $info['user_id'];
        $art = Db::name('artcomment')->where('user_id', $user_id)->select();
        $posts = Db::name('post')->where('user_id', $user_id)->select();

        $session = Session::get('user_info');
        $data = Db::name('post')->find($id);//帖子本身数据
        $pu = Db::name('index_user')->where('user_id',$data['user_id'])->find();//帖子的用户名
        $userdata = Db::name('index_user')->find($session['user_id']);//游客本身数据
        $posthot = Db::name('post')->where('posthot',0)->select();//精华帖数据
        $count = Db::name('postcomment')->where('post_id',$id)->count();//帖子 评论个数
        $postcom = Db::name('postcomment')->where('post_id',$id)->select();//帖子评论内容
        // var_dump($postcom);
        // die;
        $prcomment = Db::name('prcomment')->select();
        //每运行一次方法增加一次帖子浏览次数
        $views = $data['views'];
        $views = $views + 1 ;

        $postdata = [
            'views' => $views
        ];
        Db::name('post')->where('post_id',$id)->update($postdata);
        //每运行一次方法增加一次文章浏览次数
        $links = Db::name('links')->select();
        $roles = array();
        foreach ($postcom as $val)
        {
            $user = Db::name('index_user')->where('user_id',$val['user_id'])->find();
            $prcomment = Db::name('prcomment')->where('com_id',$val['postcom_id'])->select();
            // var_dump($prcomment);
            $prcdata = array();
            foreach($prcomment as $v)
            {
                $prcuser = Db::name('index_user')->where('user_id',$v['user_id'])->find();
                // var_dump($prcuser['username']);
                $v['prcuser'] = $prcuser['username'];
                $prcdata[] = $v;
                // var_dump($prcdata);
                // die;
            }
            // var_dump($prcdata);
            // die;
            $val['username'] = $user['username'];
            $val['userpic'] = $user['userpic'];
            $val['prcom'] = $prcdata;
            $roles[] = $val;

        }
        //把用户名,头像加进序列中
        // var_dump($roles);
        // die;
        return view('post/detail',[
            'data' => $data, //文章本身数据
            'pu' => $pu['username'],
            'posthot' => $posthot, //精华帖数据
            'userdata' => $userdata, //游客本身数据
            'postcom' => $roles,
            'session' => $session,
            'count' => $count,
            'links' => $links,
            'user_id' => $info['user_id'],
            'username' => $info['username'],
            'sex' => $info['sex'],
            'age' => $info['age'],
            'userpic' => $info['userpic'],
            'tel' => $info['tel'],
            'email' => $info['email'],
            'integral' => $info['bp'],
            'list' => $art,
            'posts' => $posts
        ]);

    }

    public function addcoment()
    {   
        // var_dump(input('session.user_info'));
        // die;
        $session = Session::get('user_info');
        $data = [
            'post_id' => $_POST['postid'],
            'user_id' => $session['user_id'],
            'comcontent' => $_POST['com'],
            'commenttime' => date('Y-m-d h:m:s')

        ];

        //发评论增加积分
        $user = Db::name('index_user')->find($session['user_id']);
        $bp = $user['bp'] + 3;
        $userdata = [
            'bp' => $bp
        ];
        Db::name('index_user')->where('user_id',$session['user_id'])->update($userdata);
        //发评论增加积分

        $result = Db::name('postcomment')->data($data)->insert();
        $users = Db::name('index_user')->find($session['user_id']);
        if($result){
            $info['status'] = true;
            Session::delete('user_info');
            Session::set('user_info',$users);
        }else{
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }

    //帖子点赞的ajax传值
    function postlove()
    {

        $data = Db::name('post')->find($_POST['postid']);
        $love = $data['postlove'] + 1 ;
        $postdata = [
            'postlove' => $love
        ];
        $result = Db::name('post')->where('post_id',$_POST['postid'])->update($postdata);
        if($result){
            $info['status'] = true;
            $info['love'] = $love;

        }else{
            $info['status'] = false;
        }
        return $info;


    }
    //帖子评论点赞的ajax传值
    function comlove()
    {

        $data = Db::name('postcomment')->find($_POST['comid']);
        $love = $data['comlove'] + 1 ;
        $comdata = [
            'comlove' => $love
        ];
        $result = Db::name('postcomment')->where('postcom_id',$_POST['comid'])->update($comdata);
        if($result){
            $info['status'] = true;
            $info['love'] = $love;

        }else{
            $info['status'] = false;
        }
        return $info;


    }
    //帖子评论讨厌的ajax传值
    function comhit()
    {

        $data = Db::name('postcomment')->find($_POST['comid']);
        $hit = $data['comhit'] + 1 ;
        $comdata = [
            'comhit' => $hit
        ];
        $result = Db::name('postcomment')->where('postcom_id',$_POST['comid'])->update($comdata);
        if($result){
            $info['status'] = true;
            $info['hit'] = $hit;

        }else{
            $info['status'] = false;
        }
        return $info;


    }

    public function addprcoment()
    {   
        // prcom=34567&postid=7&comid=1
        // var_dump(input('session.user_info'));
        // die;
        $session = Session::get('user_info');
        $data = [
            'com_id' => $_POST['comid'],
            'post_id' => $_POST['postid'],
            'prcontent' => $_POST['prcom'],
            'user_id' => $session['user_id']

        ];

        //发评论增加积分
        $user = Db::name('index_user')->find($session['user_id']);
        $bp = $user['bp'] + 3;

        $userdata = [
            'bp' => $bp
        ];
        Db::name('index_user')->where('user_id',$session['user_id'])->update($userdata);
        //发评论增加积分
        
        $result = Db::name('prcomment')->data($data)->insert();
        $users = Db::name('index_user')->find($session['user_id']);
        if($result){
            $info['status'] = true;
            Session::delete('user_info');
            Session::set('user_info',$users);
        }else{
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }



}
